package libs

import (
	"errors"
	"fmt"

	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
	"xorm.io/core"
)

var Engine *xorm.Engine
var err error

func init() {
	InitPool()
}

func InitPool() (isSuc bool) {
	dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=%s", Config.GetString("db.username"), Config.GetString("db.password"), Config.GetString("db.host"), Config.GetString("db.name"), Config.GetString("db.charset"))
	fmt.Printf("%s---", dsn)
	Engine, err = xorm.NewEngine("mysql", dsn)
	if err != nil {
		fmt.Println(err)
		panic(errors.New("mysql连接失败"))
		return false
	}
	Engine.SetMapper(core.SameMapper{}) //支持struct为驼峰式命名，表结构中为下划线命名之间的转换。该种规则为xorm默认的Maper
	Engine.SetMaxIdleConns(5)
	Engine.SetMaxOpenConns(100)
	Engine.ShowSQL(true)
	fmt.Println("db connection setup successful")
	return true
}
